package com.morningstar.qs.api.basic.common.encrypt.md5;

import java.security.MessageDigest;

public class SecUtil {
	private static final String MD5="MD5";
	public static String encrypt(String str) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance(MD5);
			byte[] digest = md.digest(str.getBytes());
			return byte2hex(digest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String byte2hex(byte[] b) {  
        String hs = "";  
        String stmp = "";  
        for (int n = 0; n < b.length; n++) {  
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));  
            if (stmp.length() == 1){  
                hs = hs + "0" + stmp;  
            }else{  
                hs = hs + stmp;  
            }  
        }  
        return hs.toUpperCase();  
    }  
}
